package com.ypan.monitor;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.viewpagerindicator.TitlePageIndicator;

/**
 * @ClassName: MainActivity
 * @Description: 主Activity
 * @author Administrator
 * @date 2012-8-7 下午10:25:09
 */
public class MainActivity extends SherlockFragmentActivity implements
		OnPageChangeListener {

	private static final String TAG = "Uninstall";
	private static final int OffscreenPageNumber = 6;
	TitlePageIndicator mTitleIndicator;
	ViewPager mViewPager;
	MainFragmentAdapter mFragAdapter;
	BaseFragment mCurFragment;

	// private static int THEME1 = R.style.Theme_Sherlock;
	// private static int THEME2 = R.style.Sherlock.Light.DarkActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// setTheme(THEME2);
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.main_tab);
		MobclickAgent.onError(this);
		initView(savedInstanceState);
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		MainApp.sMainActivity = this;
		MobclickAgent.onResume(this);
		// 初始化数据
		mCurFragment = (BaseFragment) mFragAdapter.getItem(0);
		mCurFragment.onLoadData(true);
		super.onResume();
	}

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private void initView(Bundle savedInstanceState) {

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mFragAdapter = new MainFragmentAdapter(this,
				getSupportFragmentManager());
		mViewPager.setAdapter(mFragAdapter);
		mViewPager.setOffscreenPageLimit(OffscreenPageNumber);
		mTitleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
		mTitleIndicator.setViewPager(mViewPager);
		mTitleIndicator.setOnPageChangeListener(this);
		BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
				R.drawable.bg_striped);
		bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		getSupportActionBar().setBackgroundDrawable(bg);
	}

	@Override
	public void onPageScrollStateChanged(int pos) {

	}

	@Override
	public void onPageScrolled(int pos, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int pos) {
		BaseFragment curFragment = (BaseFragment) mFragAdapter.getItem(pos);
		curFragment.onLoadData(false);
		// Log.e("test", "onPageSelected pos = "+pos);
	}
}
